package com.superhelper.deliver.task.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import com.superhelper.common.domain.DeliverRecord;
import com.superhelper.common.domain.PushMsgType;
import com.superhelper.common.domain.TakeOutOrder;
import com.superhelper.common.mapper.DeliverRecordMapper;
import com.superhelper.common.mapper.TakeOutOrderMapper;
import com.superhelper.deliver.service.DeliverService;

@Component
public class AbnormalOrderScanTask {
    @Autowired
    private DeliverRecordMapper recordMapper;
    @Autowired
    private TakeOutOrderMapper takeOutOrderMapper;
    @Autowired
    private DeliverService deliverService;

    @Scheduled(cron = "0/5 * * * * ?")
    public void run() {
        List<Long> ids = recordMapper.queryAbnormalOrder();
        for (Long id : ids) {
            if (recordMapper.updateStatusAbnormal(id, "加小费到最大金额，超过5分钟仍然无人接单") > 0) {
                DeliverRecord entity = recordMapper.getById(id);
                TakeOutOrder order = takeOutOrderMapper.getById(entity.getOrderId());
                deliverService.publish(order, PushMsgType.DELIVER_EXCEPTION);
            }
        }
    }

}
